package business

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/business"
	businessReq "github.com/flipped-aurora/gin-vue-admin/server/model/business/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"gorm.io/gorm"
)

type InvitationService struct {
}

// CreateInvitation 创建邀请记录
// Author [piexlmax](https://github.com/piexlmax)
func (invitationService *InvitationService) CreateInvitation(invitation *business.Invitation) (err error) {
	err = global.GVA_DB.Create(invitation).Error
	return err
}

// DeleteInvitation 删除邀请记录
// Author [piexlmax](https://github.com/piexlmax)
func (invitationService *InvitationService) DeleteInvitation(invitation business.Invitation) (err error) {
	db := global.GVA_DB.Model(&business.Classify{}).Where("id = ?", invitation.ID)
	if invitation.UserId != 0 {
		db.Where("user_id = ? ", invitation.UserId)
	}
	err = db.Delete(&invitation).Error
	return err
}

// DeleteInvitation 删除问卷的邀请记录
// Author [piexlmax](https://github.com/piexlmax)
func (invitationService *InvitationService) DeleteInvitationByquestionnaireUuid(questionnaireUuid string, userId uint) (err error) {
	db := global.GVA_DB.Model(&business.Classify{}).Where("questionnaire_uuid = ?", questionnaireUuid)
	if userId != 0 {
		db = db.Where("user_id = ? ", userId)
	}
	err = db.Delete(&business.Invitation{}).Error
	return err
}

// DeleteInvitationByIds 批量删除邀请记录
// Author [piexlmax](https://github.com/piexlmax)
func (invitationService *InvitationService) DeleteInvitationByIds(ids request.IdsReq, userId uint) (err error) {
	db := global.GVA_DB.Model(&business.Invitation{}).Where("id in (?)", ids)
	if userId != 0 {
		db = db.Where("user_id = ? ", userId)
	}
	err = global.GVA_DB.Delete(&business.Invitation{}).Error
	return err
}

// UpdateInvitation 更新邀请记录
// Author [piexlmax](https://github.com/piexlmax)
func (invitationService *InvitationService) UpdateInvitation(invitation business.Invitation) (err error) {
	err = global.GVA_DB.Save(&invitation).Error
	return err
}

// GetInvitation 根据id获取邀请记录
// Author [piexlmax](https://github.com/piexlmax)
func (invitationService *InvitationService) GetInvitation(id uint, userId uint) (invitation business.Invitation, err error) {
	db := global.GVA_DB.Where("id = ? ", id)
	if userId != 0 {
		db = db.Where("user_id = ? ", userId)
	}
	err = db.First(&invitation).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return
}

// GetInvitationInfoList 分页获取邀请记录
// Author [piexlmax](https://github.com/piexlmax)
func (invitationService *InvitationService) GetInvitationInfoList(info businessReq.InvitationSearch) (list []business.Invitation, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&business.Invitation{})
	var invitations []business.Invitation
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.QuestionnaireUuid != "" {
		db = db.Where("questionnaire_uuid = ?", info.QuestionnaireUuid)
	}
	if info.Name != "" {
		db = db.Where("name LIKE ?", "%"+info.Name+"%")
	}
	if info.St != 0 {
		db = db.Where("st = ?", info.St)
	}
	if info.UserId != 0 {
		db = db.Where("user_id = ?", info.UserId)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&invitations).Error
	return invitations, total, err
}

// CreateInvitationByExcel excel创建邀请记录
// Author [piexlmax](https://github.com/piexlmax)
func (invitationService *InvitationService) CreateInvitationByExcel(questionnaireUuid string, userId uint, invitationList []*business.Invitation) (err error) {
	db := global.GVA_DB

	err = db.Transaction(func(tx *gorm.DB) error {
		tx = tx.Model(&business.Invitation{}).Where("questionnaire_uuid = ?", questionnaireUuid)
		if err != nil {
			return err
		}
		if userId != 0 {
			tx = tx.Where("user_id = ? ", userId)
		}
		err = tx.Delete(&business.Invitation{}).Error
		if err != nil {
			return err
		}
		err = tx.Create(&invitationList).Error
		if err != nil {
			return err
		}
		return nil
	})

	return err
}
